<?php

/**
 * Created by PhpStorm.
 * User: cuong_000
 * Date: 1/11/2015
 * Time: 8:28 PM
 */
class Custom_order extends Front_Controller
{

    function __construct()
    {
        parent::__construct();
        $this->load->library('form_validation');
        $this->load->model(array('Custom_order_model', 'Customer_model'));
        $query =
            "CREATE TABLE IF NOT EXISTS " . $this->db->dbprefix('custom_order') . " (
            custom_order_id int(11) NOT NULL auto_increment,
            custom_order_number varchar(60) CHARACTER SET utf8 NOT NULL,
            name varchar(255) CHARACTER SET utf8 NOT NULL,
            email varchar(255) CHARACTER SET utf8 NOT NULL,
            phone varchar(255) CHARACTER SET utf8 NOT NULL,
            address varchar(500) CHARACTER SET utf8 NOT NULL,
            datetime datetime NOT NULL,
            content text CHARACTER SET utf8 NOT NULL,
            message text CHARACTER SET utf8 NOT NULL,
            admin_note text CHARACTER SET utf8 NOT NULL,
            customer_id int(11) NULL,
            PRIMARY KEY (custom_order_id)
        ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=10;";
        $this->db->query($query);
    }

    function index()
    {
        $data = array();
        $data["posted"] = false;
        $data["page_title"] = "Đặt hàng";
        if ($this->input->post("submit")) {
            if (isset($_SESSION['form_token']) && ($this->input->post("form_token") == $_SESSION['form_token'])) {
                $config['allowed_types'] = 'xls|xlsx';
                $config['upload_path'] = 'uploads/custom_orders';
                $config['encrypt_name'] = true;
                $config['remove_spaces'] = true;
                $this->load->library('upload', $config);
                if (!$this->upload->do_upload("content")) {
                    $data["error"] = array('error' => $this->upload->display_errors());
                    $form_token = uniqid();
                    $_SESSION['form_token'] = $form_token;
                    $data["form_token"] = $form_token;
                    $data["posted"] = false;
                } else {
                    $uploadFile = $this->upload->data();
                    $data = array(
                        "name" => $this->input->post("name"),
                        "email" => $this->input->post("email"),
                        "phone" => $this->input->post("phone"),
                        "address" => $this->input->post("address"),
                        "content" => $uploadFile['file_ext'],
                        "message" => $this->input->post("message")
                    );
                    $customer = $this->go_cart->customer();
                    if (isset($customer['id'])) {
                        $data['customer_id'] = $customer['id'];
                    }
                    $id = $this->Custom_order_model->insert($data);
                    if ($id) {
                        $data["posted"] = true;
                        $data["order_number"] = $id;
                    }
                    $file_path = $uploadFile['file_path'];
                    $file = $uploadFile['full_path'];
                    $file_ext = $uploadFile['file_ext'];
                    $final_file_name = $id . $file_ext;
                    rename($file, $file_path . $final_file_name);
                    unset($_SESSION['form_token']);
                }
            } else {
                $form_token = uniqid();
                $_SESSION['form_token'] = $form_token;
                $data["form_token"] = $form_token;
                $data["posted"] = false;
            }
        } else {
            $form_token = uniqid();
            $_SESSION['form_token'] = $form_token;
            $data["form_token"] = $form_token;
        }
        $this->view_nomenu("custom_order", $data);
    }
}